<template :page.sync="page">
  <div class="el-card is-always-shadow el-card__body">

<avue-crud
  ref="crud"
  :option="option"
  :table-loading="listLoading"
  :data="reportApplyTableData"
  :page.sync="page"
  :search.sync="searchForm"
  @search-change="handleFilter"
  @selection-change="selectionChange"
  @refresh-change="search"
  @on-load="search">
  <template slot-scope="scope" slot="lineCodeSearch">
    <el-select v-model="searchForm.lineCode" placeholder="线路" @change="changeLine" clearable filterable>
      <el-option
        v-for="(item,index) in lineCodes"
        :key="index"
        :label="item.name"
        :value="item.code">
      </el-option>
    </el-select>
  </template>
  <template slot="stationCodeSearch" slot-scope="scope">
    <el-select v-model="searchForm.stationCode" placeholder="车站" style="width: 150px" clearable filterable>
      <el-option
        v-for="(item,index) in stationCodes"
        :key="index"
        :label="item.name"
        :value="item.code">
      </el-option>
    </el-select>
  </template>
  <template slot="search" slot-scope="{row,size}" >
    <!-- <el-form-item label="差异金额范围" labelWidth="150px" style="width:450px">
      <el-input-number v-model="searchForm.errorAmountMin" :min="0" :max="99999998" label="差异金额范围" clearable></el-input-number>~
      <el-input-number v-model="searchForm.errorAmountMax" :min="1" :max="99999999" label="差异金额范围" clearable></el-input-number>
    </el-form-item> -->
    <el-form-item label="报表名称">
      <el-select v-model="searchForm.reportNameId" placeholder="报表名称" clearable>
        <el-option
          v-for="(item,index) in reportNameIds"
          :key="index"
          :label="item.reportName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="开始时间" style="margin-left: 20px">
      <el-date-picker
        clearable
        v-model="searchForm.startReprotTime"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" style="margin-left: 50px">
      <el-date-picker
        clearable
        v-model="searchForm.endReprotTime"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
  </template>


<template slot="menuRight">
<!--    <el-button type="primary" size="mini" @click="editor(null,3,'添加')">添加</el-button>-->
<!--    <el-button type="primary" size="mini" @click="addModifyLimitTime()">提交</el-button>-->
  </template>
  <template slot-scope="scope" slot="menu">
    <el-button type="text" size="small" plain @click.stop="previewEditor(scope.row,1,'预览')">预览</el-button>
  </template>
</avue-crud>
<Editor ref="Editor"></Editor>
<PREVIEW ref="PREVIEW" ></PREVIEW>
</div>

</template>

<script>
  import Editor from './Editor.vue';
  import PREVIEW from './pre.vue';
  import {getStationByCode,getLineAndStationByCode,getLineAllAsyn,getLineCodes,getSwPmReportPath} from "../../../../../api/subwayRat/commonCheck";

  import {exportDataToExcel} from "../../../../../api/subwayRat/exportSelf";
  import {getLineLists, getStationLists} from "../../../../../api/subwayRat/reportMondify/reportMistakeInfo";
  import {
    get_xw_ReportErrorList,
    XW_SQ_other_tableOption
  } from "../../../../../api/stationTiecketManager/xianWngCJXXQR/BBCCGG/BBCCGGSQ/otherYunYing";
  export default {
    name: 'reportApplyInfo',
    components:{Editor,PREVIEW
    },
    data() {
      return {
        listLoading: true,
        searchForm:{
          lineCode:'9',
          stationCode:''
        },
        lineCodes:[],
        stationCodes:[],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条,
          isAsc: false// 是否倒序
        },
        isShowReportMistakeInfoEditor:false,//是否加载编辑页面
        reportApplyTableData: [],
        reportApplyTableCheck:[],
        option:{},
        addModifyLimitTimeVisible : false,
        reportNameIds:[],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        }
      }
    },
    created() {
      this.option=XW_SQ_other_tableOption;
       let p={};
       p.line_code='9';
        getStationByCode(p).then((res)=>{
          this.stationCodes=res.data;
          this.searchForm.stationCode='';
        })
    },
    mounted() {
      this.search();
      this.initLine();
      this.initReportNameIds();
    },
    methods: {
      initReportNameIds(){ //初始化报表名
        getSwPmReportPath().then(res=>{
          this.reportNameIds=res.data.data;
        })
      },
      previewEditor(row,showType,title){
        this.$nextTick(()=>{
          this.$refs.PREVIEW.previewEditor(row,showType,title);
        });

      },
      editor(row,showType,title){
        this.$nextTick(()=>{
          this.$refs.Editor.previewEditor(row,showType,title);
        });
      },
      changeLine(v){
        let p={};
        // p.userCode=this.$store.getters.userInfo.userCode;
        p.line_code=v;
        getStationByCode(p).then((res)=>{
          debugger
          this.stationCodes=res.data;
          this.searchForm.stationCode='';
        })
      },
      async examinationApproval(){
        if(this.reportApplyTableCheck.length == 0){
          this.$message({message: '请勾选数据后审批',type: 'warning'});
          return;
        }
        let messageLSList = [];
        for(let i=0;i<this.selectKeys.length;i++){
          let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
          messageLSList.push({
            "content":"车站：" + data.data[0].name,
            "lineCode":this.selectKeys[i].lineCode,
            "stationCode":this.selectKeys[i].stationCode
          })
        }
        var ids=[];
        this.reportApplyTableCheck.forEach(function (key, val) {
          ids[val] = key.id
        })
        let p = {
          messageLSList:JSON.stringify(messageLSList),
          title:this.$store.getters.tag.label,
          msgType:"审批",
          type:"reportApplyInfo_001",
          ids : JSON.stringify(ids)
        };
        const h = this.$createElement;
        this.$msgbox({
          title: '消息',
          message: h('p', null, [
            h('span', null, '请选择通过或不通过 '),
          ]),
          showCancelButton: true,
          confirmButtonText: '审批通过',
          cancelButtonText: '审批不通过',
          beforeClose: (action, instance, done) => {
            if (action == 'confirm') {
              p.checkStatus = "3";
            } else{
              p.checkStatus = "4";
            }
            updateReportApplyStatus(p).then((res)=>{
              if(res.data.code == "1"){
                this.$message({
                  message: '审批完成',
                  type: 'success'
                });
                this.search();
              }
            });
            done();
          }
        }).then(action => {
        });
      },
      search(){
        console.log(this.$store.getters.userInfo.userCode);
        Object.assign(this.page,this.searchForm);
        let p={};
        Object.assign(p,this.page);
        p.userCode=this.$store.getters.userInfo.userCode;
        get_xw_ReportErrorList(p).then((res)=>{
          this.reportApplyTableData=res.data.data;
          this.page = res.data;
          this.page.data = null;
          this.listLoading=false;
        });
      },
      clear(){
        this.searchForm = {};
        // this.searchForm.errorAmountMin = 0;
        // this.searchForm.errorAmountMax = 1;
      },
      addModifyLimitTime(){
        if(this.reportApplyTableCheck.length == 0){
          this.$message({message: '请勾选数据后设定期限',type: 'warning'});
          return;
        }
        this.addModifyLimitTimeVisible = true;
        this.$nextTick(()=>{
          this.$refs.addModifyLimitTimeVue.checkData = this.reportApplyTableCheck;
        });
      },
      async exportExcel(){ // 导出
        let p={};
        Object.assign(p,this.searchForm);
        p.pageNo=1;
        p.pageSize=1000000000;
        if(this.multipleSelectionRows.length>0){
          exportDataToExcel(this.multipleSelectionRows,this.option.column,'线路报表差错更改通知-车站');
        }else {
          let res=await get_xw_ReportErrorList(p);
          let data = res.data.data;
          exportDataToExcel(data,this.option.column,'线路报表差错更改通知-车站');
        }
      },
      selectionChange(list){
        this.reportApplyTableCheck=list;
      },
      handleFilter(params, done) {
        Object.assign(this.searchForm,params);
        this.search()
        done()
      },
      initLine(){
        let p={};
        // p.userCode=this.$store.getters.userInfo.userCode;
        // getLineAllAsyn(p).then((res)=>{
        //   this.lineCodes=res.data;
        // });
        getLineCodes().then(res=>{ //获取线路
            this.lineCodes=res;
        });
      }
    }
  }
</script>
<style lang="scss">

</style>
